﻿//==============================================================================
// File: PropertyInfo.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using WpfRcp.PropertyModel.Validation;

namespace WpfRcp.PropertyModel
{
    /// <summary>
    /// Represents an information object of a given property.
    /// </summary>
    public class PropertyInfo : ValidatableBase
    {
        private object m_Value;
        private MetaPropertyInfo m_MetaProperty;

        /// <summary>
        /// Gets a value indicating whether object is in valid state.
        /// </summary>
        public override bool IsValid
        {
            get
            {
                return MetaProperty != null &&
                       MetaProperty.IsValid;
            }
        }

        /// <summary>
        /// Gets or sets the value associated with this property.
        /// </summary>
        public object Value
        {
            get { return m_Value; }
            set
            {
                if (!Equals(m_Value, value))
                {
                    m_Value = value;
                    OnPropertyChanged<PropertyInfo>(t => t.Value);
                }
            }
        }

        /// <summary>
        /// Gets or sets <see cref="MetaPropertyInfo"/> object which represents the property itself.
        /// </summary>
        public MetaPropertyInfo MetaProperty
        {
            get { return m_MetaProperty; }
            set
            {
                if (m_MetaProperty != null)
                    throw new InvalidOperationException();

                m_MetaProperty = value;
                m_MetaProperty.ObjectStateChanged += OnObjectStateChangedHandler;

                OnPropertyChanged<PropertyInfo>(t => t.MetaProperty);
            }
        }

        private void OnObjectStateChangedHandler(object sender, EventArgs e)
        {
            OnObjectStateChanged();
        }
    }
}